package com.hengxinguotong.zhihuichengjian.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.BuildMenuAdapter;
import com.hengxinguotong.zhihuichengjian.ui.app.BaseFragment;
import com.hengxinguotong.zhihuichengjian.bean.Menu;
import com.hengxinguotong.zhihuichengjian.bean.MenuBean;
import com.hengxinguotong.zhihuichengjian.bean.MenuListRes;
import com.hengxinguotong.zhihuichengjian.ui.building.BuildListActivity;
import com.hengxinguotong.zhihuichengjian.ui.building.NoticeListActivity;
import com.hengxinguotong.zhihuichengjian.ui.building.ReplyListEnterpriseActivity;
import com.hengxinguotong.zhihuichengjian.utils.Constants;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2019/1/11
 */

public class BuildMenuFragment extends BaseFragment implements BuildMenuAdapter.OnItemClickListener{
    private View rootView;
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.menu_rv)
    RecyclerView menuRv;

    private List<Menu> menuList;
    private BuildMenuAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_build, null);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    @Override
    public void onLazyLoad() {

    }

    private void initView() {
        titleTv.setText("建设管理");

        getData();
        adapter = new BuildMenuAdapter(getContext(), menuList);
        adapter.setOnItemClickListener(this);
        menuRv.setAdapter(adapter);
        menuRv.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        menuRv.setItemAnimator(new DefaultItemAnimator());
    }

    private void getData() {
        menuList = new ArrayList<Menu>();
        String authority = SPUtil.getString(getContext(), "authority");
        if(!Utils.isEmpty(authority)){
            MenuListRes menuListRes = new Gson().fromJson(authority, MenuListRes.class);
            if(menuListRes.getValue() != null && menuListRes.getValue().size() > 0){
                for (MenuBean menu:menuListRes.getValue()){
                    if(menu.getData() != null && menu.getData().size() > 0){
                        for (MenuBean m:menu.getData()){
                            if(m.getId() == 1510){
                                menuList.add(new Menu("任务下发", R.mipmap.item_renwuxiafa, "Task assignment"));
                            }else if(m.getId() == 1520){
                                menuList.add(new Menu("任务回复", R.mipmap.item_renwuhuifu, "Mission recovery"));
                            }else if(m.getId() == 1530){
                                menuList.add(new Menu("通知公告", R.mipmap.item_tongzhigonggao, "Notice announcement"));
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void onItemClick(View view, Object o, int position) {
        Intent intent = new Intent();
        switch (position){
            case 0:
                intent.setClass(getContext(), BuildListActivity.class);
                break;
            case 1:
                intent.setClass(getContext(), ReplyListEnterpriseActivity.class);
                break;
            case 2:
                intent.setClass(getContext(), NoticeListActivity.class);
                break;
        }
        intent.putExtra("fromEnterprise", true);
        intent.putExtra("title", menuList.get(position).getTitle());
        intent.putExtra("type", Constants.LIVE);
        startActivity(intent);
    }
}
